package com.wx.mall.operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.mall.operate.dto.RebDisDto;
import com.wx.mall.operate.dto.RebDiscountDto;
import com.wx.mall.operate.dto.RebToDisDto;
import com.wx.mall.operate.dto.RebateSaveDto;
import com.wx.mall.operate.entity.RebDiscount;
import com.wx.mall.operate.entity.Rebate;
import com.wx.mall.operate.mapper.RebDiscountMapper;
import com.wx.mall.operate.service.IRebDiscountService;
import com.wx.mall.operate.service.IRebateService;
import com.wx.mall.operate.vo.RebToDisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author z
 * @since 2020-6-4
 */
@Service
public class RebDiscountServiceImpl extends ServiceImpl<RebDiscountMapper, RebDiscount> implements IRebDiscountService {

    @Autowired
    IRebateService rebateService;

    @Autowired
    IRebDiscountService discountService;

    @Override
    public List<RebToDisVo> findRebDisList(RebToDisDto dto) {
        List<RebToDisVo> list=new ArrayList<>();
        QueryWrapper<RebDiscount> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("rebate_id",dto.getIds());
        List<RebDiscount> list1 = this.list(queryWrapper);
        RebToDisVo rebToDisVo=new RebToDisVo();
        rebToDisVo.setId(dto.getIds());
        Rebate byId = rebateService.getById(rebToDisVo.getId());
        rebToDisVo.setId(byId.getId());
        rebToDisVo.setTitle(byId.getTitle());
        rebToDisVo.setStartTime(byId.getStartTime());
        rebToDisVo.setEndTime(byId.getEndTime());
        rebToDisVo.setActivityState(byId.getActivityState());
        rebToDisVo.setCategory(byId.getCategory());
        rebToDisVo.setDiscountId(byId.getDiscountId());
        rebToDisVo.setPutType(byId.getPutType());
        rebToDisVo.setList(list1);
        list.add(rebToDisVo);
        return list;
    }

    @Override
    public int updateDiscount(Long ids,List<RebDiscount> rebDiscount,List<RebDiscount> id) {
        QueryWrapper<RebDiscount> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("rebate_id",ids);
        List<RebDiscount> list = this.list(queryWrapper);
        int ma=1;
        for (RebDiscount dis:rebDiscount
        ) {
            for (RebDiscount das:list
                 ) {
                RebDiscount discount=new RebDiscount();
                if (dis.getId()==das.getId()){
                    discount.setId(dis.getId());
                    discount.setLevel(dis.getLevel());
                    discount.setMode(dis.getMode());
                    discount.setRebateId(dis.getRebateId());
                    discount.setThreshold(dis.getThreshold());
                    boolean b = discountService.updateById(discount);
                    if (b==true){
                        ma=1;
                    }else {
                        ma=0;
                    }
                }else if (dis.getId()==null){
                    discount.setLevel(dis.getLevel());
                    discount.setMode(dis.getMode());
                    discount.setRebateId(dis.getRebateId());
                    discount.setThreshold(dis.getThreshold());
                    boolean save = discountService.save(discount);
                    if (save==true){
                        ma=1;
                    }else {
                        ma=0;
                    }
                }else if (das.getId()!=dis.getId()){
                    for (RebDiscount idm:id
                         ) {
                        boolean rem = discountService.removeById(idm.getId());
                        if (rem==true){
                            ma=1;
                        }else {
                            ma=0;
                        }
                    }
                }
            }
        }
        return ma;
    }

    @Override
    public int deleteDiscount(Long ids) {
        QueryWrapper<RebDiscount> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("rebate_id",ids);
        List<RebDiscount> list = this.list(queryWrapper);
        int mm=1;
        for (RebDiscount rds:list
             ) {
            boolean b = discountService.removeById(rds.getId());
            if (b==true){
                mm=1;
            }else {
                mm=0;
            }
        }
        return mm;
    }
}
